/**
 *
 */
package com.fortune.rainshine.system.controller;

import java.util.Set;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fortune.rainshine.common.BaseController;
import com.fortune.rainshine.common.utils.StringUtil;
import com.fortune.rainshine.system.service.CustomerService;
import com.fortune.rainshine.system.service.RedisService;
import com.fortune.rainshine.system.service.SmsSendFeign;
import com.fortune.rainshinecommon.base.MsgModel;
import com.fortune.rainshinecommon.base.ParmsModel;
import com.fortune.rainshinecommon.enums.RedisConstantEnum;
import com.fortune.rainshinecommon.enums.ResultCode;
import com.fortune.rainshinecommon.model.CustomerModel;
import com.fortune.rainshinecommon.model.SmsParamsModel;
import com.fortune.rainshinecommon.model.system.Role;
import com.fortune.rainshinecommon.util.RandomNumUtil;
import com.google.gson.Gson;

/**
 * @author 张鑫
 * @ClassName: CustomerController
 * @Description: TODO(会员卡管理)
 * @phone 17561783855
 * @date 2019年5月15日
 */
@Controller
@RequestMapping("/system/customer")
public class CustomerController extends BaseController {
	private static final Logger logger = LoggerFactory.getLogger(CustomerController.class);

	@Autowired
	private CustomerService service;
	@Autowired
	private RedisService redisFeign;
	@Autowired
	private SmsSendFeign smsSendFeign;
	@Value("${smsSend.number}")
	private Integer number;

	@RequiresPermissions("customer:view")
	@RequestMapping()
	public String view(Model model) {
		// 获取用户权限
		Set<String> authorities = getLoginUserAuthorities();
		// 获取登录用户的按钮展示情况
		model.addAttribute("add", authorities.contains("customer:add"));
		model.addAttribute("update", authorities.contains("customer:update"));
		model.addAttribute("delete", authorities.contains("customer:delete"));
		model.addAttribute("back", authorities.contains("customer:back"));
		return "customer/customerList.html";
	}

	@RequestMapping("/editForm")
	public String edit(Model model) {
		return "customer/customer_form.html";
	}

	/**
	 * @Title: add
	 * @Description: 添加
	 * @author zhangxin
	 * @phone 17561783855
	 * @date 2019年5月15日
	 */
	@RequiresPermissions("customer:add")
	@ResponseBody
	@RequestMapping("/add")
	public MsgModel add(@RequestBody CustomerModel model) {
		try {
			Boolean exists = redisFeign.exists(RedisConstantEnum.ADDCUSTOMER.getMessage() + model.getCustomerPhone());
			if (exists) {
				String sms = (String) redisFeign
						.get(RedisConstantEnum.ADDCUSTOMER.getMessage() + model.getCustomerPhone()).getData();
				if (sms.equals(model.getVerificationCode())) {
					model.setUser(this.getLoginUser());
					redisFeign.delete(RedisConstantEnum.ADDCUSTOMER.getMessage() + model.getCustomerPhone());
					return service.add(model);
				} else {
					return new MsgModel(ResultCode.FAILURE, "验证码不正确");
				}
			} else {
				return new MsgModel(ResultCode.FAILURE, "验证码不存在或者已过期");
			}
		} catch (Exception e) {
			logger.error("customer add ====> {}", e);
			return new MsgModel(ResultCode.FAILURE);
		}
	}

	/**
	 * @Title: list
	 * @Description: 列表
	 * @author zhangxin
	 * @date 2019年3月26日
	 */
	@RequiresPermissions("customer:view")
	@ResponseBody
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public MsgModel list(Integer page, Integer limit, String name, String phone, Integer shopId) {
		try {
			if (page == null) {
				page = 0;
				limit = 0;
			}
			Role role = getLoginUserRole();
			ParmsModel parm = new ParmsModel(page, limit);
			parm.setName(name);
			parm.setPhone(phone);
			// 判断若为超级管理员加载全部数据
			if (role.getRoleId() == 1 || role.getRoleId() == 2) {
				parm.setRoleId(role.getRoleId());
			} else {
				parm.setShopId(shopId);
			}
			return service.list(parm);
		} catch (Exception e) {
			logger.error("customer list ====> {}", e);
			return new MsgModel(ResultCode.FAILURE);
		}

	}

	@RequiresPermissions("customer:update")
	@ResponseBody
	@RequestMapping("/update")
	public MsgModel update(@RequestBody CustomerModel model) {
		try {
			/*
			 * Role role = getLoginUserRole(); if (role.getRoleId() != 2 && role.getRoleId()
			 * != 5) { Boolean exists = redisFeign
			 * .exists(RedisConstantEnum.ADDCUSTOMER.getMessage() +
			 * model.getCustomerPhone()); if (exists) { String sms = (String) redisFeign
			 * .get(RedisConstantEnum.ADDCUSTOMER.getMessage() +
			 * model.getCustomerPhone()).getData();
			 * redisFeign.delete(RedisConstantEnum.ADDCUSTOMER.getMessage() +
			 * model.getCustomerPhone()); if (!sms.equals(model.getVerificationCode())) {
			 * return new MsgModel(ResultCode.FAILURE, "验证码不正确"); } } else { return new
			 * MsgModel(ResultCode.FAILURE, "验证码不存在或者已过期"); } }
			 */
			model.setUser(this.getLoginUser());
			return service.update(model);
		} catch (Exception e) {
			logger.error("customer update ====> {}", e);
			return new MsgModel(ResultCode.FAILURE);
		}
	}

	/**
	 * @Title: delete
	 * @Description: 删除
	 * @author zhangxin
	 * @phone 17561783855
	 * @date 2019年3月28日
	 */
	@RequiresPermissions("customer:delete")
	@ResponseBody
	@RequestMapping("/delete")
	public MsgModel delete(@RequestBody CustomerModel model) {
		try {
			return service.delete(model);
		} catch (Exception e) {
			logger.error("customer delete ====> {}", e);
			return new MsgModel(ResultCode.FAILURE);
		}
	}

	/**
	 * @Title: delete
	 * @Description: 恢复
	 * @author zhangxin
	 * @phone 17561783855
	 * @date 2019年3月28日
	 */
	@RequiresPermissions("customer:back")
	@ResponseBody
	@RequestMapping("/back")
	public MsgModel back(@RequestBody CustomerModel model) {
		try {
			return service.back(model);
		} catch (Exception e) {
			logger.error("customer delete ====> {}", e);
			return new MsgModel(ResultCode.FAILURE);
		}
	}

	/**
	 * @Title: sms
	 * @Description: 添加顾客时验证码发送
	 * @author zhangxin
	 * @phone 17561783855
	 * @date 2019年5月21日
	 */
	@RequiresPermissions("customer:sms")
	@ResponseBody
	@RequestMapping("/sms")
	public MsgModel sms(@RequestBody CustomerModel model) {
		try {
			String phone = model.getCustomerPhone();
			Boolean exists = redisFeign.exists(RedisConstantEnum.ADDCUSTOMER.getMessage() + phone);
			if (exists) {
				redisFeign.delete(RedisConstantEnum.ADDCUSTOMER.getMessage() + phone);
			}
			Gson gson = new Gson();
			String code = RandomNumUtil.getRandNum(number);
			SmsParamsModel smsParams = new SmsParamsModel();
			smsParams.setCode(code);
			String jsonParams = gson.toJson(smsParams);

			Boolean bo = smsSendFeign.sendSms(phone, jsonParams);
			if (bo) {
				redisFeign.set(RedisConstantEnum.ADDCUSTOMER.getMessage() + phone, code, 60000);
			}
			return new MsgModel(ResultCode.SUCCESS);
		} catch (Exception e) {
			logger.error("customer sms ====> {}", e);
			return new MsgModel(ResultCode.FAILURE);
		}
	}

}
